#include "store.h"

Store::Store(QWidget *parent)
    : QWidget(parent)
{
    QPushButton *typeLeft = new QPushButton;
    QPushButton *typeRight = new QPushButton;
    typeLeft->setIcon(QIcon(":images/left.png"));
    typeRight->setIcon(QIcon(":images/right.png"));
    typeLeft->setIconSize(QSize(40,40));
    typeRight->setIconSize(QSize(40,40));
    typeLeft->setStyleSheet("background-color:transparent;border:none;");
    typeRight->setStyleSheet("background-color:transparent;border:none;");

    QListWidget *typeList = new QListWidget;
    typeList->setFlow(QListWidget::LeftToRight);
    typeList->setWrapping(false);
    typeList->setFixedHeight(46);
    typeList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    typeList->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    typeList->setStyleSheet("background-color:transparent;border:none;");
    for(int i=0;i<30;i++){
        QListWidgetItem *item = new QListWidgetItem;
        item->setSizeHint(QSize(60,40));
        typeList->addItem(item);
        QPushButton *btn = new QPushButton(QString("Item %1").arg(i+1));
        btn->setStyleSheet("color:white;");
        typeList->setItemWidget(item, btn);
    }

    QHBoxLayout *typeLayout = new QHBoxLayout;
    typeLayout->setContentsMargins(0,0,0,0);
    typeLayout->setSpacing(0);
    typeLayout->addWidget(typeLeft);
    typeLayout->addWidget(typeList);
    typeLayout->addWidget(typeRight);

    search = new QLineEdit;
    search->setFixedHeight(32);
    search->setClearButtonEnabled(true);
    search->setPlaceholderText("名称/条码/货号/简拼");
    search->setStyleSheet("background-color:transparent;");
    QHBoxLayout *searchLayout = new QHBoxLayout;
    searchLayout->setContentsMargins(0,0,0,0);
    searchLayout->addWidget(search);

    storeList = new QListWidget;
    // storeList->setFixedHeight(940);
    storeList->setMaximumHeight(9999);
    storeList->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    storeList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    storeList->setSortingEnabled(true);
    storeList->setAcceptDrops(false);
    storeList->setDragEnabled(false);
    storeList->setWrapping(true);
    storeList->setIconSize(QSize(230,96));
    storeList->setViewMode(QListView::IconMode);
    storeList->setSelectionMode(QAbstractItemView::SingleSelection);
    storeList->sortItems(Qt::DescendingOrder);
    storeList->setResizeMode(QListWidget::Adjust);
    // storeList->setFlow(QListWidget::LeftToRight);
    // storeList->setMaximumSize(QSize(999999,999999));    

    storeList->setStyleSheet("background-color:transparent;");
    for(int i=0;i<10;i++){
        QListWidgetItem *item = new QListWidgetItem;        
        storeList->addItem(item);
        item->setSizeHint(QSize(234,96));
        storeList->addItem(item);
        // QPushButton *btn = new QPushButton(QString("Item %1").arg(i+1));
        // btn->setStyleSheet("color:white;");
        storeList->setItemWidget(item, new Seller);
    }

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->setContentsMargins(5,5,5,5);
    mainLayout->addLayout(typeLayout);
    mainLayout->addLayout(searchLayout);
    mainLayout->addWidget(storeList);
    setLayout(mainLayout);

}

Store::~Store()
{

}

void Store::reSizeMainContainer(int height)
{
    QPoint pos = Store::storeList->pos(); // 在父窗口中的位置 x(),y()
    int ih = height - pos.y() - 50;
    storeList->setFixedHeight(ih);
    // QListWidgetItem *item = new QListWidgetItem(QString("窗口高度: %1").arg(height)+QString("列表位置: %1").arg(pos.y())+QString("列表高度: %1;").arg(ih));
    // storeList->addItem(item);
}


// ============================================
Seller::Seller(QWidget *parent)
    : QPushButton(parent)
{
    resize(QSize(230,96));

    img = new QLabel;
    QVBoxLayout *bodyLayout = new QVBoxLayout;
    title = new QLabel;title->setText("aaaaaa");
    subTitle = new QLabel;subTitle->setText("bbbb");
    spec = new QLabel;
    price = new QLabel;
    sell = new QLabel;
    bodyLayout->setContentsMargins(0,0,0,0);
    bodyLayout->addWidget(title);
    bodyLayout->addWidget(subTitle);
    bodyLayout->addWidget(spec);
    bodyLayout->addStretch();
    bodyLayout->addWidget(price);
    bodyLayout->addWidget(sell);
    QVBoxLayout *backLayout = new QVBoxLayout;
    unit = new QLabel;
    nums = new QLabel;
    backLayout->setContentsMargins(0,0,0,0);
    backLayout->addWidget(unit);
    backLayout->addStretch();
    backLayout->addWidget(nums);

    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->setContentsMargins(5,5,5,5);
    mainLayout->addWidget(img);
    mainLayout->addLayout(bodyLayout);
    mainLayout->addLayout(backLayout);
    setLayout(mainLayout);
}
Seller::~Seller()
{

}
void Seller::setBaseData(QJsonObject *data)
{

}
